package com.linmo.questionmatcher;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;

import java.util.HashSet;
import java.util.Set;

public class JaccardSimilarity {
    public static double similarity(String s1, String s2) {
        Set<String> set1 = new HashSet<>();
        Set<String> set2 = new HashSet<>();

        //使用hanlp 分词器对句子进行分词, 并去除停用词
        for (Term term : HanLP.segment(s1)) {
            set1.add(term.word);
        }
        for (Term term : HanLP.segment(s2)) {
            set2.add(term.word);
        }

        //计算交集和并集
        Set<String> intersection = new HashSet<>(set1);
        intersection.retainAll(set2);
        Set<String> union = new HashSet<>(set1);
        union.addAll(set2);

        return intersection.size() / (double) union.size();
    }
}
